//index.js
//获取应用实例
var App = getApp()
Page({
  data: {
    cccc: "",
    ks: "",
    js: "",
    motto: 'Hello World',
    merchants: {},
    prompt: {
      hidden: !0,
    },
    dates: [],
    defaultDate: '',
    defaultTime: '',
    defaultWeek: '',
    productStatus: 0
  },
  onLoad(option) {
    console.log(option, "22222222")
    wx.setStorageSync('gsfid', option.gfid);
    this.setData({
      canshu: option.can,
      sj : option.sj
    })

    let agent = wx.getStorageSync('agent');
    let position = wx.getStorageSync('position');
    let userId = wx.getStorageSync("userId");
    let products = wx.getStorageSync("products");
    var ol = wx.getStorageSync('defaultCar').carInfo
    var im = ol.trim().split(" ")
    var c = wx.getStorageSync("products")
    var d = option.productStatus
    if (d == 0) {
      this.setData({
        qian: wx.getStorageSync("qian"),
        sczdj: wx.getStorageSync("yuanjia"),
        chaxing: im[1]
      })
    } else {
      var f = wx.getStorageSync("group")
      this.setData({
        qian: f.price,
        sczdj: f.qian,
        chaxing: "各种车辆"
      })
    }

    if (option.year) {
      let defaultDate = option.year + '-' + option.month + '-' + option.day;
  
      var selectDate = new Date((defaultDate.replace(/-/g, "/")));
      let arr = [];
      for (let i = 0; i < 5; i++) {
        let today = new Date();
        let date = selectDate
        console.log("dateda", date)
        let obj = {};
        let a = date.setDate(date.getDate() + i);
        let b = new Date(a);
        let year = b.getFullYear();
        let month = b.getMonth() + 1;
        let day = b.getDate();
        let week = b.getDay();
        obj.date = month + '-' + day;
        obj.fullDate = year + '-' + month + '-' + day;
    
        switch (week) {
          case 0:
            obj.week = "周日";
            break;
          case 1:
            obj.week = "周一";
            break;
          case 2:
            obj.week = "周二";
            break;
          case 3:
            obj.week = "周三";
            break;
          case 4:
            obj.week = "周四";
            break;
          case 5:
            obj.week = "周五";
            break;
          case 6:
            obj.week = "周六";
            break;
          default:
            break;
        }
        console.log("66666arr", arr)
        if (today.getTime() == b.getTime() && i == 0) {
          obj.week = "今天";
        }
        if (today.getTime() != b.getTime() && i == 0) {
          obj.className = "active";
          this.setData({
            defaultWeek: obj.week
          });
          console.log("目标错误位置", "obj", obj)

        } else {
          obj.className = '';
        }

        arr.push(obj);
        this.setData({
          dates: arr,
          defaultDate
        });
      }
    } else {

      let today = new Date();
      // 标记修改  +1
      let nextDay = today.setDate(today.getDate());
      let year = new Date(nextDay).getFullYear();
      let month = new Date(nextDay).getMonth() + 1;
      let day = new Date(nextDay).getDate();
      
      console.log(day,"day")
      let defaultDate = year + '-' + month + '-' + day;
   
 
      // let defaultDate = "2020-10-15"

      let arr = [];
      for (let i = 0; i < 5; i++) {
        let date = new Date();
        let obj = {};
        let a = date.setDate(date.getDate() + i);
        let b = new Date(a);
        let year = b.getFullYear();
        let month = b.getMonth() + 1;
        let day = b.getDate();
        let week = b.getDay();
        obj.date = month + '-' + day;
        obj.fullDate = year + '-' + month + '-' + day;


        switch (week) {
          case 0:
            obj.week = "周日";
            break;
          case 1:
            obj.week = "周一";
            break;
          case 2:
            obj.week = "周二";
            break;
          case 3:
            obj.week = "周三";
            break;
          case 4:
            obj.week = "周四";
            break;
          case 5:
            obj.week = "周五";
            break;
          case 6:
            obj.week = "周六";
            break;
          default:
            break;
        }

        if (date.getTime() == b.getTime() && i == 0) {
          obj.week = "今天";
        }

        if (i == this.data.sj) {
          obj.className = "active";
          if(this.data.sj==1){

          //  var  defaultDate = obj.fullDate

          }
      
          // console.log(obj,"objbo")
          // console.log(this.data.sj, "this.data.sj")

         
          this.setData({
            defaultWeek: obj.week
          });
        } else {
          obj.className = '';
        }
        arr.push(obj);
      }



      this.setData({
        dates: arr,
        defaultDate
      });



    }


    this.setData({
      agent,
      position,
      userId,
      productStatus: option.productStatus,

    });

    this.onPullDownRefresh();
  },
  gouxu: function() {
    wx.showToast({
      title: '不可预约',
      image: '../../../assets/images/warning.png',
      duration: 1000
    })
  },
  initData() {
    const merchants = {
      items: [],
      total: '',
      params: {
        page: 1,
        pageCount: 10
      }
    }
    this.setData({
      merchants: merchants
    })

  },

  getList(cb) {

    let {
      merchants,
      agent,
      position
    } = this.data;
    let params = merchants.params;
    let location = position.location;
    params.sellerId = agent.id;
    params.userId = this.data.userId;

    params.lng = location.split(',')[0];

    params.lat = location.split(',')[1];

    params.dataTime = this.data.defaultDate
   


    App.HttpService.getMerchants(params).then(res => {
        cb && cb()
        let data = res.data.data
        //...es6用法 数组扩展用法
        var sss = params.page
        if (sss > 1) {
          merchants.items = [...merchants.items, ...data.shopList]
        } else {
          merchants.items = [...data.shopList]
        }
        if (data.shopList.length > 0) {
          params.page = params.page + 1
          merchants.total = data.counts
        }
        this.setData({
          merchants
        })
      },
      e => {
        cb && cb()
      })
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading()

    this.initData()

    this.getList((res) => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom() {
    let merchants = this.data.merchants
    if (merchants.items.length === merchants.total) {
      return
    }
    this.getList()
  },
  toMerchant(e) {
    // let {id, name} = e.currentTarget.dataset
    // wx.navigateTo({
    //  url: `/pages/merchants/detail/detail?merchantId=${id}&merchantName=${name}`
    // })
  },
  booking(e) {
    let {
      id,
      name,
      phone,
      address
    } = e.currentTarget.dataset
    let {
      productStatus
    } = this.data
    if (productStatus == 0) { // 普通商品选择店铺
      this.data.productStatus = productStatus;

      wx.navigateTo({
        url: `/pages/order/confirm/index?merchantId=${id}&merchantName=${name}&phone=${phone}&address=${address}`
      })

    } else if (productStatus == 1) { // 拼团商品选择店铺
      this.data.productStatus = productStatus;
      wx.navigateTo({
        url: `/pages/group-buying/confirm/index?merchantId=${id}&merchantName=${name}`
      })
    } else if (productStatus == 2) { // 套餐商品选择店铺
      this.data.productStatus = productStatus;
      wx.navigateTo({
        url: `/pages/order/confirm/index?merchantId=${id}&merchantName=${name}`
      })
    }
  },
  // 跳转到日历
  toCalendar() {
    wx.navigateTo({
      url: `/pages/merchants/calendar/index?date=${this.data.defaultDate}&productStatus=` + this.data.productStatus,
    })
  },
  //  选择日期
  chooseDate(e) {
  
    let index = e.currentTarget.dataset.index;
    let date = e.currentTarget.dataset.date;
    let week = e.currentTarget.dataset.week;
  


    for (let i = 0; i < 5; i++) {
      let className = "dates[" + i + "].className";

      this.setData({
        [className]: ""
      });


    }

    if (e.currentTarget.dataset.week == "今天") {
      // 修改问题
      // index += 1;
      let className = "dates[" + index + "].className";
      this.setData({
        [className]: 'active',
        defaultDate: date
      });
      // return;
      // 修改问题、
    }
  



    let className = "dates[" + index + "].className";
    this.setData({
      [className]: 'active',
      defaultDate: date,
      defaultWeek: week
    });


    var merchants = this.data.merchants;

    merchants.params.page = 1;
    this.setData({
      merchants: merchants
    })

    this.getList();
  },
  // 选择时间
  bindTimeChange(e) {
    this.setData({
      defaultTime: e.detail.value
    });
    let {
      id,
      name,
      address,
      phone,
      distance
    } = e.currentTarget.dataset
    let {
      productStatus
    } = this.data

    let date = this.data.defaultDate;
    let week = this.data.defaultWeek;
    let time = this.data.defaultTime;

    if (productStatus == 0) {
      // 普通商品选择店铺
      var canshu = this.data.canshu
      if (canshu == "undefined") {

        //没有参数信息
        wx.navigateTo({
          url: `/pages/order/confirm/index?merchantId=${id}&merchantName=${name}&date=${date}&week=${week}&time=${time}&address=${address}&phone=${phone}&distance=${distance}`
        })
      } else {


        //有参数信息
        wx.navigateTo({
          url: `/pages/order/confirme/index?merchantId=${id}&merchantName=${name}&date=${date}&week=${week}&time=${time}&address=${address}&phone=${phone}&distance=${distance}&canshu=${canshu}`
        })
      }




    } else if (productStatus == 1) { // 拼团商品选择店铺
      wx.navigateTo({
        url: `/pages/group-buying/confirm/index?merchantId=${id}&merchantName=${name}&date=${date}&week=${week}&time=${time}&address=${address}&phone=${phone}&distance=${distance}`
      })
    } else if (productStatus == 2) { // 套餐商品选择店铺
      wx.navigateTo({
        url: `/pages/order/confirm/index?merchantId=${id}&merchantName=${name}&date=${date}&week=${week}&time=${time}&address=${address}&phone=${phone}&distance=${distance}`
      })
    }
  },

  bodadianhua: function(e) {
    console.log(e.currentTarget.dataset.s)
    let dh = e.currentTarget.dataset.s
    wx.makePhoneCall({
      phoneNumber: dh,
    })
  },
  jrxq: function(e) {
    console.log(e)
    let shopId = e.currentTarget.dataset.id;
    let distance = e.currentTarget.dataset.distance;
    wx.navigateTo({
      url: `/pages/merchantsList/detail/detail?shopId=${shopId}&distance=${distance}`,
    })

  },
  onShow(e) {

    var _this = this
    var params = 'merchants.params.page'

    this.setData({
      [params]: 1

    })


    if (this.data.cccc) {
      let defaultDate = this.data.cccc

      var selectDate = new Date((defaultDate.replace(/-/g, "/")));
      let arr = [];
      for (let i = 0; i < 5; i++) {
        let today = new Date();

        let date = selectDate
        console.log("dateda666666666te", date)

        let obj = {};
        let a = date.setDate(date.getDate() + i);
        let b = new Date(a);
        let year = b.getFullYear();
        let month = b.getMonth() + 1;
        let day = b.getDate();
        let week = b.getDay();
        obj.date = month + '-' + day;
        obj.fullDate = year + '-' + month + '-' + day;

        switch (week) {
          case 0:
            obj.week = "周日";
            break;
          case 1:
            obj.week = "周一";
            break;
          case 2:
            obj.week = "周二";
            break;
          case 3:
            obj.week = "周三";
            break;
          case 4:
            obj.week = "周四";
            break;
          case 5:
            obj.week = "周五";
            break;
          case 6:
            obj.week = "周六";
            break;
          default:
            break;
        }
        console.log("66666arr", arr)
        if (today.getTime() == b.getTime() && i == 0) {
          obj.week = "今天";
        }
        if (today.getTime() != b.getTime() && i == 0) {
          obj.className = "active";
          this.setData({
            defaultWeek: obj.week
          });
          console.log("目标错误位置", "obj", obj)

        } else {
          obj.className = '';
        }

        arr.push(obj);

        this.setData({
          dates: arr,
          defaultDate
        });

      }
      // 重新吊起請求店鋪
      console.log("分页字段", _this.data.merchants.params.page)
      this.getList()
    }
  }
})